﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using CrazyWorship1.Class_Files.PropertyHandler;
using System.Data;
using CrazyWorship1.Class_Files;

namespace CrazyWorship1.Windows
{
    /// <summary>
    /// Interaction logic for SongEditor.xaml
    /// </summary>
    public partial class SongEditor : Window
    {
        public SongEditor()
        {
            InitializeComponent();
        }

        private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
        {

        }

        private void textBox3_TextChanged(object sender, TextChangedEventArgs e)
        {

        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
        }
        private void EnsureExtraTextBoxesExist()
        {
            if (stackPanel1.Children.Count == 0)
                AddExtraTextBox();

            StackPanel lyricContainer = (StackPanel)stackPanel1.Children[stackPanel1.Children.Count - 1];
            Grid gridContainer = (Grid)lyricContainer.Children[0];
            if (((TextBox)gridContainer.Children[0]).Text != "")
                AddExtraTextBox();
        }
        private void AddExtraTextBox()
        {
            StackPanel lyricContainer = new StackPanel();
            Grid a = new Grid();
            a.ColumnDefinitions.Add(new ColumnDefinition());
            a.ColumnDefinitions.Add(new ColumnDefinition());
            a.ColumnDefinitions.Add(new ColumnDefinition());
           
            a.ColumnDefinitions[0].Width = new GridLength(128, GridUnitType.Star);
            a.ColumnDefinitions[1].Width = new GridLength(50);
            a.ColumnDefinitions[2].Width = new GridLength(50);

            TextBox tb = new TextBox();
            tb.TextChanged += new TextChangedEventHandler(textBox5_TextChanged);
            tb.Margin = new Thickness(0, 0, 0, 0);
            ComboBox cb = new ComboBox();
            cb.Margin = new Thickness(0, 0, 0, 0);
            Button bt = new Button();
            bt.Margin = new Thickness(0, 0, 0, 0);
            bt.Content = "X";
            bt.Click += new RoutedEventHandler(button1_Click_1);
            a.Children.Add(tb);
            a.Children.Add(cb);
            a.Children.Add(bt);
            Grid.SetColumn(tb, 0);
            Grid.SetColumn(cb, 1);
            Grid.SetColumn(bt, 2);
            lyricContainer.Children.Add(a);

            Expander ex=new Expander();
                ex.Margin=new Thickness(10,0,0,0);
                ex.FontSize=10;
                ex.Header="Lyric Transitions";
                StackPanel exsp=new StackPanel();
                    
                    StackPanel exspa=new StackPanel();
                        exspa.Orientation=Orientation.Horizontal;
                        Label exspal=new Label();
                        exspal.Content="Transition In:";
                        ComboBox lyricTransitionIn = new ComboBox();
                        exspa.Children.Add(exspal);
                        exspa.Children.Add(lyricTransitionIn);
                    StackPanel exspb=new StackPanel();
                    StackPanel exspc=new StackPanel();
                        exspc.Orientation=Orientation.Horizontal;
                        Label exspcl=new Label();
                        exspcl.Content="Transition Out:";
                        ComboBox lyricTransitionOut=new ComboBox();
                        exspc.Children.Add(exspcl);
                        exspc.Children.Add(lyricTransitionOut);
                    StackPanel exspd=new StackPanel();

                    exsp.Children.Add(exspa);
                    exsp.Children.Add(exspb);
                    exsp.Children.Add(exspc);
                    exsp.Children.Add(exspd);

            ex.Content = exsp;
            lyricContainer.Children.Add(ex);
            

            stackPanel1.Children.Add(lyricContainer);

            lyricTransitionIn.DataContext = CurrentState.GetInTransitions();
            lyricTransitionIn.ItemsSource = CurrentState.GetInTransitions();
            lyricTransitionIn.SelectionChanged += new SelectionChangedEventHandler(lyricTransitionIn_SelectionChanged);
            lyricTransitionOut.DataContext = CurrentState.GetOutTransitions();
            lyricTransitionOut.ItemsSource = CurrentState.GetOutTransitions();
            lyricTransitionOut.SelectionChanged += new SelectionChangedEventHandler(lyricTransitionOut_SelectionChanged);

        }

        void lyricTransitionOut_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ComboBox senderCombo=(ComboBox)sender;
            StackPanel lyricTransitionOutGrid = (StackPanel)((StackPanel)((StackPanel)senderCombo.Parent).Parent).Children[3];
            lyricTransitionOutGrid.Children.Clear();
            // a =CurrentState.GetCreatorControls((string)Creator.SelectedValue);
            propertyHandlers = CurrentState.GetOutTransition((string)senderCombo.SelectedValue).PropertyHandlers;
            foreach (KeyValuePair<string, PropertyHandlerControl> b in propertyHandlers)
            {
                StackPanel sp = new StackPanel();
                sp.Orientation = Orientation.Horizontal;
                Label lb1 = new Label();
                lb1.Content = b.Key + ": ";
                sp.Children.Add(lb1);
                sp.Children.Add(b.Value);
                lyricTransitionOutGrid.Children.Add(sp);
            }
        }

        void lyricTransitionIn_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ComboBox senderCombo=(ComboBox)sender;
            StackPanel lyricTransitionInGrid = (StackPanel)((StackPanel)((StackPanel)senderCombo.Parent).Parent).Children[1];
            lyricTransitionInGrid.Children.Clear();
            // a =CurrentState.GetCreatorControls((string)Creator.SelectedValue);
            propertyHandlers = CurrentState.GetInTransition((string)senderCombo.SelectedValue).PropertyHandlers;
            foreach (KeyValuePair<string, PropertyHandlerControl> b in propertyHandlers)
            {
                StackPanel sp = new StackPanel();
                sp.Orientation = Orientation.Horizontal;
                Label lb1 = new Label();
                lb1.Content = b.Key + ": ";
                sp.Children.Add(lb1);
                sp.Children.Add(b.Value);
                lyricTransitionInGrid.Children.Add(sp);
            }
        }

        private void button1_Click_1(object sender, RoutedEventArgs e)
        {
            stackPanel1.Children.Remove((UIElement)((Button)sender).Parent);
            EnsureExtraTextBoxesExist();

        }

        private void textBox5_TextChanged(object sender, TextChangedEventArgs e)
        {
            EnsureExtraTextBoxesExist();
        }

        private void textBox1_GotFocus(object sender, RoutedEventArgs e)
        {

        }

        private void SongName_GotFocus(object sender, RoutedEventArgs e)
        {
            if (this.songName.Text == "Song Name...")
            {
                songName.Text = "";
                songName.Foreground = Brushes.Black;
            }
        }

        private void songName_LostFocus(object sender, RoutedEventArgs e)
        {
            if (this.songName.Text == "")
            {
                songName.Text = "Song Name...";
                songName.Foreground = Brushes.DarkGray;
            }

        }

        private void Author_GotFocus(object sender, RoutedEventArgs e)
        {
            if (this.Author.Text == "Author...")
            {
                Author.Text = "";
                Author.Foreground = Brushes.Black;
            }
        
        }

        private void Author_LostFocus(object sender, RoutedEventArgs e)
        {
            if (this.Author.Text == "")
            {
                Author.Text = "Author...";
                Author.Foreground = Brushes.DarkGray;
            }

        }

        private void Year_GotFocus(object sender, RoutedEventArgs e)
        {
            if (this.Year.Text == "Year...")
            {
                Year.Text = "";
                Year.Foreground = Brushes.Black;
            }
        
        }

        private void Year_LostFocus(object sender, RoutedEventArgs e)
        {
            if (this.Year.Text == "")
            {
                Year.Text = "Year...";
                Year.Foreground = Brushes.DarkGray;
            }

        }

        private void Copyright_GotFocus(object sender, RoutedEventArgs e)
        {
            if (this.Copyright.Text == "Copyright...")
            {
              Copyright.Text = "";
              Copyright.Foreground = Brushes.Black;
            }
        
        }

        private void Copyright_LostFocus(object sender, RoutedEventArgs e)
        {
            if (this.Copyright.Text == "")
            {
                Copyright.Text = "Copyright...";
                Copyright.Foreground = Brushes.DarkGray;
            }

        }
        State _currentState;
        public State CurrentState
        {
            get
            {
                return _currentState;
            }
            set
            {
                _currentState = value;
                comboBox1.ItemsSource = _currentState.SongDB.PresetSong;
                comboBox1.DataContext = _currentState.SongDB.PresetSong;
                comboBox1.DisplayMemberPath = "Name";
                comboBox1.SelectedValuePath = "PresetSongID";
                
            }
        }
        private bool _isEdit=false;
        public bool IsEdit
        {
            get
            {
                return _isEdit;
            }
        }
        public bool IsNew {
            get
            {
                return !IsEdit;
            }
        }
        Guid editSongId;
        public void SetEditSong(Guid songId)
        {
            editSongId = songId;
            _isEdit=true;
        }
        private void button1_Click_2(object sender, RoutedEventArgs e)
        {
           
                if (songName.Text != "Song Name...")
                {
                    SongDB songdb = new SongDB();
                    SongDB.SongsRow r;
                    if (IsNew)
                        r = songdb.Songs.AddSongsRow(Guid.NewGuid(), this.songName.Text, this.Author.Text, Creator.SelectedValue.ToString(), "", "");
                    else
                        r = songdb.Songs.AddSongsRow(editSongId, this.songName.Text, this.Author.Text, Creator.SelectedValue.ToString(), "", "");

                    int ord = 0;

                    //SET LYRICS
                    SongDB.LyricsRow lr=null;
                    foreach (StackPanel grd in stackPanel1.Children)
                    {
                        string txt = ((TextBox)((Grid)grd.Children[0]).Children[0]).Text;
                       
                        StackPanel transitions = ((StackPanel)((Expander)grd.Children[1]).Content);
                        ComboBox inTrans = (ComboBox)((StackPanel)transitions.Children[0]).Children[1];
                        ComboBox outTrans = (ComboBox)((StackPanel)transitions.Children[2]).Children[1];

                        lr = songdb.Lyrics.AddLyricsRow(r, Guid.NewGuid(), txt, ord, "", (string)inTrans.SelectedValue, (string)outTrans.SelectedValue);


                       
                        
                       
                        
                        ord++;

                    }
                    songdb.Lyrics.RemoveLyricsRow(lr);

                    //SET PROPERTY HANDLERS
                    foreach (KeyValuePair<string, PropertyHandlerControl> prop in propertyHandlers)
                    {
                        songdb.CreatorAppProperties.AddCreatorAppPropertiesRow(Guid.NewGuid(), r, prop.Key, prop.Value.Value, "Creator");
                        
                    }


                   // DataUtils.ApplyPresetToSong(CurrentState.SongDB.PresetSong.FindByPresetSongID((Guid)comboBox1.SelectedValue), r, songdb);
                    songdb.WriteXml(songdb.Songs[0].SongID.ToString().Replace("{","").Replace("}","")+".sng");
                    if (IsEdit)
                        CurrentState.SongDB.Songs.RemoveSongsRow(CurrentState.SongDB.Songs.FindBySongID(editSongId));
                    CurrentState.SongDB.Merge(songdb);
                }
                this.Close();


        }

        private void expander1_Expanded(object sender, RoutedEventArgs e)
        {

        }

        private void expander3_Expanded(object sender, RoutedEventArgs e)
        {

        }
        Window _parentForm;
        public Window ParentForm
        {
            get
            {
                return _parentForm;
            }
            set
            {
                _parentForm = value;
                _parentForm.Closing += new System.ComponentModel.CancelEventHandler(_parentForm_Closing);
            }
        }

        void _parentForm_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            this.Close();
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Creator.DataContext = CurrentState.GetCreators();
            Creator.ItemsSource = CurrentState.GetCreators();
            SongTransitionIn.DataContext = CurrentState.GetSongInTransitions();
            SongTransitionIn.ItemsSource = CurrentState.GetSongInTransitions();
            SongTransitionOut.DataContext = CurrentState.GetSongOutTransitions();
            SongTransitionOut.ItemsSource = CurrentState.GetSongOutTransitions();
            LyricTransitionIn.ItemsSource = CurrentState.GetInTransitions();
            LyricTransitionIn.DataContext = CurrentState.GetInTransitions();
            LyricTransitionOut.ItemsSource = CurrentState.GetOutTransitions();
            LyricTransitionOut.DataContext = CurrentState.GetOutTransitions();

            LyricTransitionIn.SelectionChanged += new SelectionChangedEventHandler(lyricTransitionIn_SelectionChanged);
            LyricTransitionOut.SelectionChanged += new SelectionChangedEventHandler(lyricTransitionOut_SelectionChanged);
            Creator.SelectionChanged += new SelectionChangedEventHandler(Creator_SelectionChanged);
            SongTransitionIn.SelectionChanged += new SelectionChangedEventHandler(SongTransitionIn_SelectionChanged);
            SongTransitionOut.SelectionChanged += new SelectionChangedEventHandler(SongTransitionOut_SelectionChanged);


            if (IsEdit)
                LoadSong();
        }

    
        private void LoadSong()
        {

        }

        void SongTransitionOut_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            songTransitionOutGrid.Children.Clear();
            // a =CurrentState.GetCreatorControls((string)Creator.SelectedValue);
            propertyHandlers = CurrentState.GetSongOutTransition((string)SongTransitionOut.SelectedValue).PropertyHandlers;
            foreach (KeyValuePair<string, PropertyHandlerControl> b in propertyHandlers)
            {
                StackPanel sp = new StackPanel();
                sp.Orientation = Orientation.Horizontal;
                Label lb1 = new Label();
                lb1.Content = b.Key + ": ";
                sp.Children.Add(lb1);
                sp.Children.Add(b.Value);
                songTransitionOutGrid.Children.Add(sp);
            }
        }

        void SongTransitionIn_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            songTransitionInGrid.Children.Clear();
            // a =CurrentState.GetCreatorControls((string)Creator.SelectedValue);
            propertyHandlers = CurrentState.GetSongInTransition((string)SongTransitionIn.SelectedValue).PropertyHandlers;
            foreach (KeyValuePair<string, PropertyHandlerControl> b in propertyHandlers)
            {
                StackPanel sp = new StackPanel();
                sp.Orientation = Orientation.Horizontal;
                Label lb1 = new Label();
                lb1.Content = b.Key + ": ";
                sp.Children.Add(lb1);
                sp.Children.Add(b.Value);
                songTransitionInGrid.Children.Add(sp);
            }
        }
        Dictionary<string, PropertyHandlerControl> propertyHandlers;
        void Creator_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            creatorGrid.Children.Clear();
            // a =CurrentState.GetCreatorControls((string)Creator.SelectedValue);
            propertyHandlers = CurrentState.GetCreator((string)Creator.SelectedValue).PropertyHandlers;
            foreach (KeyValuePair<string, PropertyHandlerControl> b in propertyHandlers)
            {
                StackPanel sp=new StackPanel();
                sp.Orientation=Orientation.Horizontal;
                Label lb1 = new Label();
                lb1.Content = b.Key + ": "; 
                sp.Children.Add(lb1);
                sp.Children.Add(b.Value);
                creatorGrid.Children.Add(sp);
            }
        }

        private void CCLIImportButton_Click(object sender, RoutedEventArgs e)
        {
          
                string textImport = "";
                if (Clipboard.ContainsText())
                    textImport = Clipboard.GetText();

                DecodeSongFromWeb decoder = new DecodeSongFromWeb(textImport);
                
                this.songName.Text = decoder.SongTitle;
                this.Author.Text = decoder.Author;
                this.Copyright.Text = decoder.CopyWrite;
                List<string> newlyrics = decoder.GetLyrics();
                int i = 0;
                foreach (string lyric in newlyrics)
                {
                    Grid grd = (Grid)stackPanel1.Children[i];
                     ((TextBox)grd.Children[0]).Text=lyric;
                     i++;
                }

               
        }

        private void expander2_Expanded(object sender, RoutedEventArgs e)
        {

        }


    }
}
